function repair(m: string | number) {
  return m < 10 ? '0' + m : m
}
export function formatting(time: string) {
  var times = new Date(time);
  var y = times.getFullYear();
  var m = times.getMonth() + 1;
  var d = times.getDate();
  var h = times.getHours();
  var mm = times.getMinutes();
  var s = times.getSeconds();
  return y + '-' + repair(m) + '-' + repair(d) + ' ' + repair(h) + ':' + repair(mm) + ':' + repair(s);
}

class Events{
  _events: any;
  constructor(){
      this._events=Object.create(null)
  }
  $on(event:string,callback:Function){  //监听event事件，触发时调用callback函数
      let callbacks = this._events[event] || [] //判断event事件是否已有其他事件处理函数，没有则为空数组
      callbacks.push(callback)
      this._events[event] = callbacks
      return this
  }

  $emit(...args:any[]){ //触发事件，并把参数传给事件的处理函数
      const event = args[0]
      const params = Array.prototype.slice.call(args,1) //!!!因为args非数组，所以需要通过空数组来调用数组的slice方法并把this指向args
      const callbacks = this._events[event]//获取event事件的全部事件处理函数
      callbacks.forEach((fn:any) => fn(...params))//遍历执行全部事件处理函数
      return this
  }
  once(event:string,callback:Function){
      let watch=(...args:any[])=>{
          callback(...args)
          this.off(event,watch)
      }
      this.$on(event,watch)
      return this
  }
  off(event:string,callback:Function){
          if(!this._events[event]){
              return 
          }
          this._events[event]=this._events[event].filter((fn: Function)=>fn!==callback)
          return this
  }
}
let vEvent=new Events()
export default vEvent